import {createRouter, createWebHistory} from 'vue-router'

import Home from "@/pages/home/Home.vue";
import NotFound from "@/components/NotFound.vue";
import Search from "@/pages/Search.vue";
import UserSpace from "@/pages/UserSpace.vue";
import Download from "@/pages/Download.vue";
import ChannelMenu from "@/pages/home/ChannelMenu.vue";
import HomeVideo from "@/pages/home/HomeVideo.vue";
import ChannelVideo from "@/pages/channeVideo/ChannelVideo.vue";
import PlayVideo from "@/pages/playVideo/PlayVideo.vue";
import SubChannelMenu from "@/pages/channeVideo/SubChannelMenu.vue";


import useNavInfoStore from "@/stores/nav";
import {getNavValue} from "@/api/api";
import routerName from "@/stores/router-name";

const routes = [
    {
        path:'/',
        // name:'home',
        component: Home,
        children:[
            {
                path:'',
                component: ChannelMenu,
                children:[
                    {
                        path:'',
                        name:'home',
                        component: HomeVideo,
                    },
                    {
                        path:'/v',
                        component: SubChannelMenu,
                        children: [
                            {
                                path:'/:englishName/:subEnglishName',
                                name:'channel_video',
                                component: ChannelVideo,
                            },
                        ],

                    }
                ]
            },
            {
                path:'UserSpace',
                name:'user_space',
                component: UserSpace,
            },
            {
                path:'/play_video/:video_id',
                name:'play_video',
                component: PlayVideo,
            },
        ]
    },
    {
        path:'/Search',
        name:'search',
        component: Search,
    },
    {
        path:'/Download',
        name:'download',
        component: Download,
    },
    {
        path:'/:pathMatch(.*)*',
        name:'not_found',
        component: NotFound
    }
]


const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeResolve(async (to, from, next) => {
    if(useNavInfoStore().menus.length === 0) {
        console.log('获取 NavMenus 数据')
        try {
            let savedNavValue = localStorage.getItem('navMenus');

            if (savedNavValue !== 'undefined' && savedNavValue !== null) {
                savedNavValue = JSON.parse(savedNavValue);
                useNavInfoStore().setNavValue(savedNavValue);
            } else {
                useNavInfoStore().setNavValue(await getNavValue());
                localStorage.setItem('navMenus', JSON.stringify(useNavInfoStore().menus));
            }
        }catch (error){
            console.error("Failed to fetch navigation menus:", error);
            next({name: routerName().notFound})
            return;
        }
    }
    next();
})

export default router